﻿using System.Collections;
using System.Collections.Generic;
using Extensions;
using GameFramework.Fsm;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityGameFramework.Runtime;

namespace StarForce {
    public class CardHoverState : BasicCardState
    {
        private Vector3 StartPosition { get; set; }
        private Vector3 StartEuler { get; set; }
        private Vector3 StartScale { get; set; }

        public CardHoverState(Card handler, UiCardParameters parameters) : base(handler,parameters) {
            
        }

        protected override void OnEnter(IFsm<Card> fsm) {
            base.OnEnter(fsm);
            MakeRenderFirst();
            SubscribeInput();
            CachePreviousValues();
            SetScale();
            SetPosition();
            SetRotation();
        }

        protected override void OnLeave(IFsm<Card> fsm, bool isShutdown) {
            ResetValue();
            UnSubscribeInput();
            DisableCollision();
            base.OnLeave(fsm, isShutdown);
        }

        private void SetPosition() {
            Camera camera = Hander.MainCamera;
            Vector3 halfCardHeight = new Vector3(0,Hander.SpriteRenderer.bounds.size.y/2,0);
            Vector3 bottomEdge = Hander.MainCamera.ScreenToWorldPoint(Vector3.zero);
            Vector3 topEdge = Hander.MainCamera.ScreenToWorldPoint(new Vector3(0, Screen.height));
            int edgeFactor = Hander.transform.CloserEdge(camera, Screen.width, Screen.height);
            Vector3 myEdge = edgeFactor == 1 ? bottomEdge : topEdge;
            Vector3 edgeY = new Vector3(0,myEdge.y,0);
            var currentPosWithoutY = new Vector3(Hander.transform.position.x, 0, Hander.transform.position.z);
            var hoverHeightParameter = new Vector3(0, Parameters.HoverHeight);
            var final = currentPosWithoutY + edgeY + (halfCardHeight + hoverHeightParameter) * edgeFactor;
            Hander.MoveTo(final, Parameters.HoverSpeed);
        }

        private void SetScale() {
            Vector3 currentScale = Hander.transform.localScale;
            Vector3 finalScale = currentScale * Parameters.HoverScale;
            Hander.ScaleTo(finalScale,Parameters.ScaleSpeed);
        }

        private void SetRotation() {
            float speed = Parameters.RotationSpeed;
            Hander.RotateTo(Vector3.zero, speed);
        }

        private void ResetValue() {
            float rotation = Parameters.RotationSpeed;
            Hander.ScaleTo(StartScale,Parameters.ScaleSpeed);
            Hander.MoveTo(StartPosition,Parameters.HoverSpeed);
            Hander.RotateTo(StartEuler, rotation);
        }

        private void CachePreviousValues() {
            StartPosition = Hander.transform.position;
            StartEuler = Hander.transform.eulerAngles;
            StartScale = Hander.transform.localScale;
        }

        private void SubscribeInput() {
            Hander.Input.OnPointerExit += OnPointerExit;
            Hander.Input.OnPointerDown += OnPointerDown;
        }

        private void UnSubscribeInput() {
            Hander.Input.OnPointerExit -= OnPointerExit;
            Hander.Input.OnPointerDown -= OnPointerDown;
        }

        void OnPointerExit(PointerEventData obj) {
            if (Fsm.CurrentState == this) {
                ChangeState<CardIdleState>(Fsm);
            }
        }

        void OnPointerDown(PointerEventData eventData) {
            if (Fsm.CurrentState == this) {
                ChangeState<CardSelectState>(Fsm);
            }
        }
    }
}
